package com.sghd.fight.module.game.config;

import com.sghd.common.ramcache.IEntity;
import com.sghd.common.ramcache.PerPersist;
import com.sghd.common.ramcache.anno.Cached;
import com.sghd.common.ramcache.anno.CachedSizes;
import com.sghd.common.ramcache.anno.Persister;
import com.sghd.common.ramcache.anno.Persisters;
import com.sghd.common.utils.json.JsonUtils;
import com.sghd.pojo.fight.game.ElementAttr;
import com.sghd.pojo.fight.game.RoomType;

import javax.persistence.*;

/**
 * @author frank
 * @date 2018-12-20 11:27
 * @desc 五行属性配置
 **/
@Entity
@Table(catalog = "bsdld_fight")
@Cached(size = CachedSizes.DEFAULT, persister = @Persister(Persisters.PRE_HOUR))
public class ElementConfig implements IEntity<ElementPk>, PerPersist {

    @EmbeddedId
    private ElementPk id;

    @Transient
    private ElementAttr pvpAttr;
    @Lob
    private String pvpAttrJson;
    @Transient
    private ElementAttr pveAttr;
    @Lob
    private String pveAttrJson;

    @Override
    public ElementPk getId() {
        return id;
    }

    public ElementAttr getPvpAttr() {
        return pvpAttr;
    }

    public void setPvpAttr(ElementAttr pvpAttr) {
        this.pvpAttr = pvpAttr;
    }

    public String getPvpAttrJson() {
        return pvpAttrJson;
    }

    public void setPvpAttrJson(String pvpAttrJson) {
        this.pvpAttrJson = pvpAttrJson;
    }

    public ElementAttr getPveAttr() {
        return pveAttr;
    }

    public ElementAttr getAttr(RoomType type) {
        if (type == RoomType.PVP) {
            return pvpAttr;
        }
        return pveAttr;
    }

    public void setPveAttr(ElementAttr pveAttr) {
        this.pveAttr = pveAttr;
    }

    public String getPveAttrJson() {
        return pveAttrJson;
    }

    public void setPveAttrJson(String pveAttrJson) {
        this.pveAttrJson = pveAttrJson;
    }

    public void setId(ElementPk id) {

        this.id = id;
    }

    @Override
    public void afterLoad() {
        pvpAttr = JsonUtils.string2Object(pvpAttrJson, ElementAttr.class);
        pveAttr = JsonUtils.string2Object(pveAttrJson, ElementAttr.class);
    }

    @Override
    public void beforePersist() {
        pvpAttrJson = JsonUtils.object2String(pvpAttr);
        pveAttrJson = JsonUtils.object2String(pveAttr);
    }

    public ElementConfig copy() {
        ElementConfig copyOf = new ElementConfig();
        copyOf.id = this.id;
        copyOf.pvpAttr = this.pvpAttr;
        copyOf.pveAttr = this.pveAttr;
        return copyOf;
    }
}
